package com.reeygu.base;

import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * 演示线程池三种常用分类
 */
public class pool {
    /**
     * 他们的底层原理都是去new ThreadPoolExecutor来实现的
     * @param args
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Please enter the execution number:");
        int select = scanner.nextInt();
        switch (select) {
            //一池多线程创建一个5个线程的线程池对象
            case 1: demo1(); break;
            //一池一线程
            case 2: demo2(); break;
            //一池可扩容的线程
            case 3: demo3(); break;
        }
    }

    public static void demo1() {
        //一池多线程创建一个5个线程的线程池对象
        ExecutorService threadPoolOne = Executors.newFixedThreadPool(5);
        try {
            //10个业务需要处理
            for (int i = 0; i < 10; i++) {
                threadPoolOne.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "任务执行中...");
                    try {
                        TimeUnit.SECONDS.sleep(5);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "任务完成!");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //最好关闭线程池
            threadPoolOne.shutdown();
        }
    }

    public static void demo2() {
        //一池一线程
        ExecutorService threadPoolTow = Executors.newSingleThreadExecutor();
        try {
            //10个业务需要处理
            for (int i = 0; i < 10; i++) {
                threadPoolTow.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "任务执行中...");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "任务完成!");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //最好关闭线程池
            threadPoolTow.shutdown();
        }
    }

    public static void demo3() {
        //一池可扩容的线程
        ExecutorService threadPoolThree = Executors.newCachedThreadPool();
        try {
            //10个业务需要处理
            for (int i = 0; i < 10; i++) {
                threadPoolThree.execute(() -> {
                    System.out.println(Thread.currentThread().getName() + "任务执行中...");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "任务完成!");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //最好关闭线程池
            threadPoolThree.shutdown();
        }
    }
}
